package com.cgs.query.server.controller

import com.cgs.query.domain.ExecutionObj
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import io.swagger.annotations.ApiParam
import com.cgs.query.domain.QueryDefine
import com.cgs.query.server.service.ServerQueryService
import com.cgs.query.service.IExecutionObjService
import com.cgs.query.service.IQueryDefineService
import com.cgs.query.service.IQueryService
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/sql")
@Api(description = "SQL执行模块")
class DefineDetailsController {

    @Autowired
    private lateinit var executionObjService: IExecutionObjService

    @Autowired
    private lateinit var queryService: IQueryService

    @PostMapping("/pool/execute/{id}/{sourceId}")
    @ApiOperation(value = "通过定义执行对象的ID进行执行")
    fun execute(@ApiParam(value = "执行对象编号", required = true) @PathVariable id: String,
                @ApiParam(value = "数据源编号", required = true) @PathVariable sourceId: String,
                @ApiParam(value = "查询参数") @RequestBody(required = false) param: Map<String, Any?>,
                @ApiParam(value = "展示数量") @RequestParam(required = false, defaultValue = "7") limit: Int,
                @ApiParam(value = "超时时间") @RequestParam(required = false, defaultValue = "5") timeout: Int): Any? {
        return (queryService as? ServerQueryService)?.serverPageExecute(id, sourceId, param, limit, timeout)
    }

    @PostMapping("/project/execute/{projectName}/{id}")
    @ApiOperation(value = "通过（项目名称）执行定义")
    fun executeWithProject(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String,
                           @ApiParam(value = "执行对象编号", required = true) @PathVariable id: String,
                           @ApiParam(value = "预览参数") @RequestBody(required = false) param: Map<String, Any?>,
                           @ApiParam(value = "调用名称") @RequestParam(required = false) callName: String?,
                           @ApiParam(value = "展示数量") @RequestParam(required = false, defaultValue = "7") limit: Int,
                           @ApiParam(value = "超时时间") @RequestParam(required = false, defaultValue = "5") timeout: Int): Any? {
        return (queryService as ServerQueryService).projectPageExecute(projectName,callName, id, param, limit, timeout)
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过定义id查询所有的版本信息")
    fun findByDefineId(@ApiParam(value = "编号", required = true) @PathVariable id: String) =
            executionObjService.findByDefineId(id)

    @PostMapping("/")
    @ApiOperation(value = "修改执行信息")
    fun save(@ApiParam(value = "查询定义", required = true) @RequestBody executionObj: ExecutionObj) =
            executionObjService.save(executionObj)

    @GetMapping("/{id}/{version}")
    @ApiOperation(value = "通过定义编号、版本号查询详细信息")
    fun findByDefineIdAndVersion(@ApiParam(value = "编号", required = true) @PathVariable id: String,
                                 @ApiParam(value = "编号", required = true) @PathVariable version: String) =
            executionObjService.findByDefineIdAndVersion(id, version)

    @GetMapping("/{id}")
    @ApiOperation(value = "通过Id查询详细信息")
    fun findById(@ApiParam(value = "编号", required = true) @PathVariable id: String) = executionObjService.findById(id)

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    fun delete(@ApiParam(value = "编号", required = true) @PathVariable id: String) = executionObjService.delete(id)

    @PostMapping("/render/{id}")
    @ApiOperation(value = "查看渲染后的sql语句")
    fun render(@ApiParam(value = "执行对象编号", required = true) @PathVariable id: String,
               @ApiParam(value = "查询参数", required = false) @RequestBody(required = false) param: Map<String, Any?>) =
            executionObjService.rendered(id, param)


    @PostMapping("/render")
    @ApiOperation(value = "查看渲染后的sql语句")
    fun render(@ApiParam(value = "查询参数", required = false) @RequestBody(required = true) executionObj: ExecutionObj) =
            executionObjService.rendered(executionObj)
}

